var url = serverPre + "/manager/yzt/v1/bizModify"

var bsConf = {

    queryFormId: "biz-query-form",

    createFormId: "biz-create-form",

    updateFormId: "biz-update-form",

    tableID: "id",

    queryCols: 3,

    queryFloat: "right",

    voName: "bizInfo",
    initData: true,
    queryConfigs:                   //页面头部查询form
        [
            {
                type: 'text',
                label: '客户号',
                name: 'customerNo',
                default: '',
                verify: ''
            },
            {
                type: 'text',
                label: '证件号码',
                name: 'idCardNo',
                default: '',
                verify: ''
            },
            {
                type: 'text',
                label: '客户姓名',
                name: 'customerName',
                default: '',
                verify: ''
            },
            {
                type: 'select',
                label: '来源系统',
                name: 'subSystemSign',
                dataUrl: serverPre + "/manager/yzt/v1/system/dictInfo?accessed=1",
                nameProperty: 'systemName',
                idProperty: 'systemSign'
            },
            {
                type: 'select',
                label: '处理状态',
                name: 'status',
                default: '5',
                verify: '',
                cols: [[0, '待处理'], [1, '已变更处理'], [2, '已合并处理'], [3, '处理失败']]
            },
            {
                type: 'select',
                label: '通知状态',
                name: 'noticeStatus',
                default: '3',
                verify: '',
                cols: [[0, '未通知'], [1, '通知成功'], [2, '通知失败']]
            },
            {
                type: 'datetime',
                label: '创建时间',
                name: 'createTimeStart',
                default: '',
                verify: ''
            },
            {
                type: 'datetime',
                label: '至',
                name: 'createTimeEnd',
                default: '',
                verify: ''
            }
            // {
            //     type: 'datetime',
            //     label: '处理时间',
            //     name: 'updateTimeStart',
            //     default: '',
            //     verify: ''
            // },
            // {
            //     type: 'datetime',
            //     label: '至',
            //     name: 'updateTimeEnd',
            //     default: '',
            //     verify: ''
            // }
        ],

    //列表内容
    tableCols: [
        {type: 'checkbox'}
        , {field: 'id', title: '序号', type: 'numbers'}
        , {field: 'customerNo', width: 200, title: '客户号'}
        , {field: 'systemSign', width: 150, title: '系统标识'}
        , {field: 'subSystemSign', width: 150, title: '子系统标识'}
        , {field: 'customerName', width: 150, title: '客户姓名'}
        , {field: 'idCardTypeName', width: 100, title: '证件类型'}
        , {field: 'idCardNo', width: 180, title: '证件号码'}
        , {field: 'createTime', width: 180, title: '创建时间'}
        , {field: 'updateTime', width: 180, title: '处理时间'}
        , {field: 'status', width: 120, title: '处理状态'}
        , {field: 'noticeStatus', width: 100, title: '通知状态'}
        , {field: 'remark', width: 150, title: '备注'}
    ],
    enableBtns: true,
};

function updateBiz(obj) {
    if (obj.className.indexOf("layui-btn-disabled") < 0) {
        var checkStatus = table.checkStatus('id'),
            data = checkStatus.data;
        if (data.length === 0) {
            layer.msg('请选择一条数据处理');
            return;
        } else if (data.length > 1) {
            layer.msg('只允许处理一条数据');
            return;
        } else if (data[0].status == "已变更处理" || data[0].status == "已合并处理") {
            layer.msg('当前状态已经是终态不能进行变更处理');
            return;
        }
        var id = data[0].id;
        $.ajax({
            type: 'get',
            url: serverPre + "/manager/yzt/v1/bizModify/updateBiz?id=" + id,
            dataType: 'json',
            headers: {"token": getCookie("token")},
            async: false,
            cache: false,
            success: function (result) {
                if (result.code == 0) {
                    var tablec = "<table class=\"layui-table\">\n" +
                        "  <colgroup>\n" +
                        "    <col width=60>\n" +
                        "    <col width=60>\n" +
                        "    <col width=60>\n" +
                        "    <col>\n" +
                        "  </colgroup>\n" +
                        "  <thead>\n" +
                        "    <tr>\n" +
                        "      <th>原客户姓名</th>\n" +
                        "      <th>原证件类型</th>\n" +
                        "      <th>原证件号码</th>\n" +
                        "    </tr> \n" +
                        "  </thead>\n" +
                        "  <tbody>\n";
                    var v = result.data.oldInfo;
                    tablec += "<tr><td>" + v['customerName'] + "</td><td>" + v['idCardTypeName'] + "</td><td>" + v['idCardNo'] + "</td></tr>\n";
                    tablec += "</tbody></table>";

                    var tablec2 = "<table class=\"layui-table\">\n" +
                        "  <colgroup>\n" +
                        "    <col width=60>\n" +
                        "    <col width=60>\n" +
                        "    <col width=60>\n" +
                        "    <col>\n" +
                        "  </colgroup>\n" +
                        "  <thead>\n" +
                        "    <tr>\n" +
                        "      <th>新客户姓名</th>\n" +
                        "      <th>新证件类型</th>\n" +
                        "      <th>新证件号码</th>\n" +
                        "    </tr> \n" +
                        "  </thead>\n" +
                        "  <tbody>\n";

                    var v1 = result.data.newInfo;
                    tablec2 += "<tr><td>" + v1['customerName'] + "</td><td>" + v1['idCardTypeName'] + "</td><td>" + v1['idCardNo'] + "</td></tr>\n";
                    tablec2 += "</tbody></table>";

                    var content = "<div style='margin: 10px;'>" +
                        "<blockquote class=\"layui-elem-quote\">个人证件信息修改：</blockquote>" + tablec +
                        tablec2 +
                        "</div>";
                    var index = layer.open({
                        type: 1,
                        title: '变更处理详情信息',
                        shadeClose: true,
                        shade: false,
                        maxmin: true,
                        content: content,
                        area: ['50%', '60%'],
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            updateSts(v1['id'], v['id'], v1['customerName'], v1['idCardType'], v1['idCardNo'], v1['customerNo']);
                            layer.close(index);
                        },
                        cancel: function (index, layero) {
                            layer.close(index);
                        }
                    });
                } else {
                    layer.msg(result.msg, {
                        time: 2000
                    });
                    return;
                }
            }
        });
    }
}


function updateSts(bizId, id, customerName, idCardType, idCardNo, customerNo) {
    layer.confirm('确定处理这条数据吗?', function (index) {
        $.ajax({
            type: 'get',
            url: serverPre + "/manager/yzt/v1/bizModify/updateSts?id=" + id + "&&bizId=" + bizId + "&&customerName=" + customerName + "&&idCardType=" + idCardType + "&&idCardNo=" + idCardNo + "&&customerNo=" + customerNo,
            dataType: 'json',
            headers: {"token": getCookie("token")},
            async: false,
            cache: false,
            success: function (result) {
                if (result.code == 0 && result.data > 0) {
                    layer.msg("处理成功", {
                        icon: 1,
                        time: 1000
                    });
                    queryData(url, pageObj.curr, pageObj.limit);
                } else {
                    layer.msg(result.msg, {
                        time: 2000
                    });
                }
            }
        });

    });
}

function mergeBizInfo(obj) {
    if (obj.className.indexOf("layui-btn-disabled") < 0) {
        var checkStatus = table.checkStatus('id'),
            data = checkStatus.data;
        var bizId = data[0].id;
        if (data.length === 0) {
            layer.msg('请选择一条数据');
            return;
        } else if (data.length > 1) {
            layer.msg('只允许处理一条数据');
            return;
        } else if (data[0].status == "已变更处理" || data[0].status == "已合并处理") {
            layer.msg('当前状态已经是终态不能进行合并处理');
            return;
        }
        $.ajax({
            type: 'get',
            url: serverPre + "/manager/yzt/v1/customer/getMergeBizInfo?id=" + bizId,
            dataType: 'json',
            headers: {"token": getCookie("token")},
            async: false,
            cache: false,
            success: function (result) {
                if (result.code == 0) {
                    var newInfo = result.data.newInfo;
                    var oldInfo = result.data.oldInfo;
                    var tablec = "<div id='mergeInfo' style='margin-top: 30px;margin-left: 50px;font-size: 14px;line-height:20px'>";
                    tablec += "<tr>合并前客户信息：</tr><br/>";
                    tablec += "<tr>客户号:" + oldInfo['customerNo'] + "</tr><br/>";
                    tablec += "<tr>客户姓名:" + oldInfo['customerName'] + "</tr><br/>";
                    tablec += "<tr>证件类型:" + oldInfo['idCardTypeName'] + "</tr><br/>";
                    tablec += "<tr>证件号码:" + oldInfo['idCardNo'] + "</tr><br/><br/>";
                    tablec += "<tr>合并后客户信息：</tr><br/>";
                    tablec += "<tr>客户号:" + newInfo['customerNo'] + "</tr><br/>";
                    tablec += "<tr>客户姓名:" + newInfo['customerName'] + "</tr><br/>";
                    tablec += "<tr>证件类型:" + newInfo['idCardTypeName'] + "</tr><br/>";
                    tablec += "<tr>证件号码:" + newInfo['idCardNo'] + "</tr>";
                    tablec += "</div>";
                    var content = tablec;
                    layer.open({
                        type: 1,
                        title: '客户合并',
                        skin: 'layui-layer-molv',//样式类名
                        shadeClose: true,
                        shade: false,
                        maxmin: true,
                        content: content,
                        area: ['350px', '350px'],
                        btn: ['确定', '取消'],
                        yes: function (index, layero) {
                            var customerNo = newInfo['customerNo'];
                            var id = newInfo['id'];
                            var mergeId = oldInfo['id'];
                            var mergeCustomerNo = oldInfo['customerNo'];
                            var content1 = "<div  style='margin-top: 70px;margin-left: 30px;margin-right:30px;font-size: 14px;line-height:25px' ><span style='font-size: 14px;'>" + "将进行两条客户信息合并处理,合并后的客户号为：" + customerNo + "   ,请确认是否是同一客户,进行合并,合并后不可逆!!!" + "</span></div>";
                            var index1 = layer.open({
                                type: 1,
                                title: '客户合并',
                                skin: 'layui-layer-molv',//样式类名
                                shadeClose: true,
                                shade: false,
                                maxmin: true,
                                content: content1,
                                area: ['350px', '350px'],
                                btn: ['确定', '取消'],
                                yes: function (index1, layero) {
                                    mergeAndNotice(id, customerNo, mergeId, mergeCustomerNo, bizId);
                                    layer.close(index1);
                                },
                                cancel: function (index1, layero) {
                                    layer.close(index1);
                                }
                            });
                            layer.close(index);
                        },
                        cancel: function (index, layero) {
                            layer.close(index);
                        }
                    });
                }
                else {
                    layer.msg(result.msg, {
                        time: 2000
                    });
                }
            }
        });
    }
}

function mergeAndNotice(id, customerNo, mergeId, mergeCustomerNo, bizId) {
    $.ajax({
        type: 'get',
        url: serverPre + "/manager/yzt/v1/customer/mergeBiz?id=" + id + "&&customerNo=" + customerNo + "&&mergeId=" + mergeId + "&&mergeCustomerNo=" + mergeCustomerNo + "&&bizId=" + bizId,
        dataType: 'json',
        headers: {"token": getCookie("token")},
        async: false,
        cache: false,
        success: function (result) {
            if (result.code == 0 && result.data.status == 0) {
                layer.msg("客户信息合并处理成功", {
                    icon: 1,
                    time: 1000
                });
                queryData(url, pageObj.curr, pageObj.limit);
            } else {
                layer.msg("客户信息合并处理失败", {
                    time: 2000
                });
            }
        }
    });

}

function noticeBiz(obj) {
    if (obj.className.indexOf("layui-btn-disabled") < 0) {
        var checkStatus = table.checkStatus('id'),
            data = checkStatus.data;
        var bizId = data[0].id;
        if (data.length === 0) {
            layer.msg('请选择一条数据');
            return;
        } else if (data.length > 1) {
            layer.msg('只允许处理一条数据');
            return;
        } else if (data[0].status != "已变更处理" && data[0].status != "已合并处理") {
            layer.msg('当前处理状态不是终态，无法通知');
            return;
        }
        else if (data[0].noticeStatus == "通知成功") {
            layer.msg('当前通知状态已经是通知成功');
            return;
        }
        var customerNo = data[0].customerNo;
        var customerName = data[0].customerName;
        var idCardType = data[0].idCardType;
        var idCardNo = data[0].idCardNo;
        var type = 2;
        $.ajax({
            type: 'get',
            url: serverPre + "/manager/yzt/v1/customer/sendBizNotice?id=" + bizId + "&&customerNo=" + customerNo + "&&customerName=" + customerName + "&&idCardType=" + idCardType + "&&idCardNo=" + idCardNo + "&&type=" + type,
            dataType: 'json',
            headers: {"token": getCookie("token")},
            async: false,
            cache: false,
            success: function (result) {
                if (result.code == 0 && result.data.status == 0) {
                    layer.msg("通知成功", {
                        icon: 1,
                        time: 1000
                    });
                    queryData(url, pageObj.curr, pageObj.limit);
                } else {
                    layer.msg("通知失败", {
                        time: 2000
                    });
                }
            }
        });
    }
}

function biz_search() {
    $("#biz-query-form").toggle()
}